package com.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.stereotype.Component;

/**
 * 局部过滤器，针对具体路由
 */
@Component
public class MyAddHeaderGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {

    @Override
    public GatewayFilter apply(NameValueConfig config) {
        return (exchange, chain) -> {
            String value = ServerWebExchangeUtils.expand(exchange, config.getValue());
            exchange.getResponse().getHeaders().add(config.getName(), value);

            return chain.filter(exchange);
        };
    }
}
